package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entity.Teacher;

public class TeacherDao extends BaseSql<Teacher> {
	private Connection conn;

	public TeacherDao() {
		conn = DBUtile.getConnection();
	}

	public List<Teacher> getTeacher(String name) {
		List<Teacher> list = new ArrayList<>();
		String sql = "select * from teacher where id in(select teacher_id from teachercourse where course_id in(select id from courses where name=?))";
		try {
			PreparedStatement pstm = conn.prepareStatement(sql);
			pstm.setString(1, name);
			ResultSet rs = pstm.executeQuery();
			while (rs.next()) {
				Teacher t = new Teacher();
				t.setId(rs.getInt("id"));
				t.setName(rs.getString("name"));
				list.add(t);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

	}

}
